{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# numpy学习和笔记\n",
    "感谢这篇博客的作者，我的内容基本就是按照这篇博客来撸的。\n",
    "\n",
    "[python之numpy的基本使用](http://blog.csdn.net/cxmscb/article/details/54583415)\n",
    "\n",
    "## 概述\n",
    "numpy（Numerical Python）提供了python对多维数组对象的支持：ndarray，具有矢量运算能力，快速、节省空间。numpy支持高级大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库。\n",
    "\n",
    "## ndarry数组的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "<class 'numpy.ndarray'>\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "2\n",
      "(3, 2)\n",
      "[ 0.  0.  0.  0.  0.  0.]\n",
      "[[ 1.  1.  1.]\n",
      " [ 1.  1.  1.]]\n",
      "[[  0.00000000e+000   0.00000000e+000   0.00000000e+000   0.00000000e+000]\n",
      " [  0.00000000e+000   5.59282311e-321   1.30290542e-311   0.00000000e+000]]\n",
      "[0 1 2 3 4 5]\n",
      "[0 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "data=[1,2,3,4,5,6]\n",
    "x=np.array(data)\n",
    "print(x)\n",
    "print(type(x))\n",
    "\n",
    "data=[[1,2],[3,4],[5,6]]\n",
    "x=np.array(data)\n",
    "print(x)\n",
    "print(x.ndim) # 维度\n",
    "print(x.shape)# 各个维度的长度，数组\n",
    "\n",
    "x=np.zeros(6)\n",
    "print(x)\n",
    "x=np.ones((2,3))\n",
    "print(x)\n",
    "x=np.empty((2,4))\n",
    "print(x)\n",
    "\n",
    "x=np.arange(6)\n",
    "print(x)\n",
    "x=np.arange(0,6,3)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarry矢量化计算\n",
    "矢量运算：相同大小的数组键间的运算应用在元素上  \n",
    "矢量和标量运算： 将标量传递到各个元素\n",
    "\n",
    "**基本和代数一块的计算相同**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 4 6]\n",
      "[False False  True]\n",
      "[4 6 8]\n",
      "[ 3  8 15]\n"
     ]
    }
   ],
   "source": [
    "x=np.array([1,2,3])\n",
    "print(x*2)\n",
    "print(x>2)\n",
    "y=np.array([3,4,5])\n",
    "print(y+x)\n",
    "print(x*y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarry基础索引和切片\n",
    "尤其是高维数组的索引\n",
    "- arr[r1:r2, c1:c2]\n",
    "- arr[1,1] 等价 arr[1][1]\n",
    "- [:] 代表某个维度的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n",
      "3\n",
      "3\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "# 索引\n",
    "x=np.array([[1,2],[3,4],[5,6]])\n",
    "print(x[0])\n",
    "print(x[1][0])\n",
    "print(x[1,0])\n",
    "\n",
    "x=np.array([[[1, 2], [3,4]], [[5, 6], [7,8]]])\n",
    "y=x[0].copy()\n",
    "print(y)\n",
    "z=x[0]\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3]\n",
      "[1 3 5 7]\n",
      "[[0 2]\n",
      " [0 4]\n",
      " [5 6]]\n",
      "[[8 2]\n",
      " [6 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "x=np.array([1,2,3,4,5,6,7,8,9])\n",
    "print(x[1:3])\n",
    "print(x[0:8:2])\n",
    "\n",
    "x=np.array([[1,2],[3,4],[5,6]])\n",
    "x[:2,:1]=0 # 批量标量赋值\n",
    "print(x)\n",
    "x[:2,:1]=[[8],[6]] # 批量矢量赋值\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 布尔索引与花式索引\n",
    "布尔索引：使用布尔数组作为索引。arr[condition]，condition为一个条件/多个条件组成的布尔数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 3 3]\n",
      "[2 1]\n",
      "[ True False  True False  True]\n"
     ]
    }
   ],
   "source": [
    "x=np.array([3,2,3,1,3])\n",
    "y=np.array([True,False,True,False,True])\n",
    "print(x[y])\n",
    "print(x[y==False])\n",
    "print(x>=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "花式索引：使用整型数组作为索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 4]\n",
      "[3 6 5]\n",
      "2 [0, 1]\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "[[1 2]\n",
      " [3 4]]\n"
     ]
    }
   ],
   "source": [
    "x=np.array([1,2,3,4,5,6,3])\n",
    "print(x[[0,1,3]])\n",
    "print(x[[-1,-2,-3]])\n",
    "\n",
    "x = np.array([[1,2],[3,4],[5,6]])\n",
    "print(x[0,1],[0,1])\n",
    "print(x[[0,1]][:,[0,1]])\n",
    "print(x[np.ix_([0,1],[0,1])])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ndarry数组转置与轴兑换\n",
    "数组的转置/轴对换只会返回源数据的一个视图，不会对源数据进行修改。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 3 6]\n",
      " [1 4 7]\n",
      " [2 5 8]]\n",
      "[[  5  14  23]\n",
      " [ 14  50  86]\n",
      " [ 23  86 149]]\n",
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n",
      "[[[0 1]\n",
      "  [4 5]]\n",
      "\n",
      " [[2 3]\n",
      "  [6 7]]]\n",
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n",
      "[[[0 1]\n",
      "  [4 5]]\n",
      "\n",
      " [[2 3]\n",
      "  [6 7]]]\n",
      "[[0 1 2]\n",
      " [3 4 5]\n",
      " [6 7 8]]\n",
      "[[0 3 6]\n",
      " [1 4 7]\n",
      " [2 5 8]]\n"
     ]
    }
   ],
   "source": [
    "k=np.arange(9)\n",
    "m=k.reshape((3,3))\n",
    "\n",
    "# 转置\n",
    "print(m.T)\n",
    "\n",
    "# 内积\n",
    "print(np.dot(m,m.T))\n",
    "\n",
    "# 轴变换:由轴编号组成的元组\n",
    "k=np.arange(8).reshape(2,2,2)\n",
    "print(k)\n",
    "m=k.transpose((1,0,2))\n",
    "print(m)\n",
    "\n",
    "# 轴变换：一对轴编号\n",
    "m=k.swapaxes(0,1)\n",
    "print(k)\n",
    "print(m)\n",
    "\n",
    "# 用轴变换实现转置\n",
    "m = np.arange(9).reshape((3,3))\n",
    "print(m)\n",
    "print(m.swapaxes(1,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarray通用函数\n",
    "一元函数表\n",
    "![通用函数表一元函数](http://img.blog.csdn.net/20170116155903070?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  4  9 16 25]\n",
      "(array([ 0.5,  0.6,  0.7,  0.8]), array([ 1.,  1.,  1.,  1.]))\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(6)\n",
    "print(np.square(x))\n",
    "\n",
    "x = np.array([1.5,1.6,1.7,1.8])\n",
    "print(np.modf(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二元函数\n",
    "![二元函数](http://img.blog.csdn.net/20170116160741445?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4]\n",
      " [6 8]]\n",
      "[[1 3]\n",
      " [5 7]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1,4],[6,7]])\n",
    "y = np.array([[2,3],[5,8]])\n",
    "print(np.maximum(x,y))\n",
    "print(np.minimum(x,y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## where函数\n",
    "np.where(condition, x, y)，第一个参数为一个布尔数组，第二个参数和第三个参数可以是标量也可以是数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2  2 -2  2]\n",
      "[ 1.  2. -3. -4. -5.  0.]\n"
     ]
    }
   ],
   "source": [
    "cond = np.array([True,False,True,False])\n",
    "x = np.where(cond,-2,2)\n",
    "print(x)\n",
    "\n",
    "y1 = np.array([-1,-2,-3,-4,-5,-6])\n",
    "y2 = np.array([1,2,3,4,5,6])\n",
    "y3 = np.zeros(6)\n",
    "cond = np.array([1,2,3,4,5,6])\n",
    "x = np.where(cond>5,y3,np.where(cond>2,y1,y2))\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 常用统计方法\n",
    "![](http://img.blog.csdn.net/20170117120917206?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 去重和集合运算\n",
    "![](http://img.blog.csdn.net/20170117123631949?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 5 6]\n",
      "[ True  True False  True  True False  True  True False]\n",
      "[2 3]\n",
      "[1 5 6]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1,6,2],[6,1,3],[1,5,2]])\n",
    "print(np.unique(x))\n",
    "y = np.array([1,6,5])\n",
    "print(np.in1d(x,y))\n",
    "print(np.setdiff1d(x,y))\n",
    "print(np.intersect1d(x,y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 线性代数常用公式\n",
    "![](http://img.blog.csdn.net/20170117135917748?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 11]\n",
      " [11 25]]\n",
      "[[ 5 11]\n",
      " [11 25]]\n",
      "[[ 2. -1.]\n",
      " [-1.  1.]]\n",
      "[[ 1.  0.]\n",
      " [ 0.  1.]]\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "import numpy.linalg as nla\n",
    "x = np.array([[1,2],[3,4]])\n",
    "y = np.array([[1,3],[2,4]])\n",
    "print(x.dot(y))\n",
    "print(np.dot(x,y))\n",
    "x = np.array([[1,1],[1,2]])\n",
    "y = nla.inv(x)\n",
    "print(y)\n",
    "print(x.dot(y))\n",
    "print(nla.det(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机数生成\n",
    "![](http://img.blog.csdn.net/20170117141702475?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50034\n",
      "[[-0.30002934 -0.62865066]\n",
      " [ 0.19204104  1.28358447]]\n"
     ]
    }
   ],
   "source": [
    "import numpy.random as npr\n",
    "\n",
    "x = npr.randint(0,2,size=100000)\n",
    "print((x>0).sum())\n",
    "print(npr.normal(size=(2,2)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数组重塑"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2]\n",
      " [3 4 5]]\n",
      "[[0 1 2]\n",
      " [3 4 5]]\n",
      "[0 1 2 3 4 5] [0 1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(0,6)\n",
    "print(x.reshape((2,3)))\n",
    "\n",
    "y = np.array([[1,1,1],[1,1,1]])\n",
    "print(x.reshape(y.shape))\n",
    "\n",
    "print(x.flatten(),x.ravel())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的拆分与合并\n",
    "![](http://img.blog.csdn.net/20170117143943037?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3htc2Ni/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n",
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n",
      "[array([[1, 2, 3]]), array([[4, 5, 6]])]\n",
      "[array([[1],\n",
      "       [4]]), array([[2],\n",
      "       [5]]), array([[3],\n",
      "       [6]])]\n",
      "[[ 0.          1.        ]\n",
      " [ 2.          3.        ]\n",
      " [ 4.          5.        ]\n",
      " [-0.80761676  0.73364896]\n",
      " [-0.01971579  1.19945788]\n",
      " [ 1.9433741   0.02144263]]\n",
      "[[ 0.          1.          0.        ]\n",
      " [ 2.          3.          1.        ]\n",
      " [ 4.          5.          2.        ]\n",
      " [-0.80761676  0.73364896  3.        ]\n",
      " [-0.01971579  1.19945788  4.        ]\n",
      " [ 1.9433741   0.02144263  5.        ]]\n",
      "[[  1 -10]\n",
      " [  2  -9]\n",
      " [  3  -8]\n",
      " [  4  -7]\n",
      " [  5  -6]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "y = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "print(np.concatenate([x, y], axis = 0)  )\n",
    "print(np.concatenate([x, y], axis = 1)  )\n",
    "\n",
    "print(np.vstack((x, y)))\n",
    "print(np.hstack((x, y)))\n",
    "\n",
    "print(np.split(x,2,axis=0) )\n",
    "print(np.split(x,3,axis=1) )\n",
    "\n",
    "# 堆叠辅助类\n",
    "arr = np.arange(6)\n",
    "arr1 = arr.reshape((3, 2))\n",
    "arr2 = np.random.randn(3, 2)\n",
    "print(np.r_[arr1, arr2])\n",
    "print(np.c_[np.r_[arr1, arr2], arr])\n",
    "print(np.c_[1:6, -10:-5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 元素的重复操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 2 2 3 3 4 4]\n",
      "[[1 2]\n",
      " [1 2]\n",
      " [3 4]\n",
      " [3 4]]\n",
      "[[1 1 2 2]\n",
      " [3 3 4 4]]\n",
      "[1 2 1 2]\n",
      "[[1 2 1 2]\n",
      " [1 2 1 2]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1,2],[3,4]])\n",
    "print(x.repeat(2))\n",
    "print(x.repeat(2,axis=0))\n",
    "print(x.repeat(2,axis=1))\n",
    "\n",
    "x=np.array([1,2])\n",
    "print(np.tile(x,2))\n",
    "print(np.tile(x,(2,2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
